package wangjg.codec;

import java.awt.Canvas;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

/**
 * 
 * @author wangjigang1
 *
 */
public class ImageEditor {
	private BufferedImage srcImage;
	private Font font;
	private java.awt.Color color;
	
	public void create(int w ,int h, int imageType){
		srcImage = new BufferedImage(w, h, imageType);
		
		//test
		int rgb = (imageType==BufferedImage.TYPE_4BYTE_ABGR||imageType==BufferedImage.TYPE_INT_ARGB)?0xffff0000:0xff0000;
		for(int i=0;i<srcImage.getWidth();i++){
			for(int j=0;j<srcImage.getHeight();j++){
				srcImage.setRGB(i, j, rgb);
			}
		}
	}

	public void read(File file) throws IOException{
		srcImage = ImageIO.read(file);
	}
	
	public void read(InputStream in) throws IOException{
		srcImage = ImageIO.read(in);
	}
	
	public void write(File file) throws IOException{
		String formatName = file.getName().substring(file.getName().length()-3);
		ImageIO.write(srcImage, formatName, file);
	}
	
	public void write(OutputStream out, String formatName) throws IOException{
		ImageIO.write(srcImage, formatName, out);
	}

	public void zoom(int width, int height){
		double w = srcImage.getWidth();
		double h = srcImage.getHeight();
		
		double sx = width/w;
		double sy = height/h;
		if(sx > sy){
			sx = sy;
			width = (int) Math.round(w*sx);
		}else{
			sy = sx;
			height = (int) Math.round(h*sy);
		}

		BufferedImage destImg = new BufferedImage(width, height, srcImage.getType());
		AffineTransform transform = new AffineTransform();
		transform.setToScale(sx, sy);		
		AffineTransformOp ato = new AffineTransformOp(transform, null);
		ato.filter(this.srcImage, destImg);
		this.srcImage = destImg;
	}
	
	public void crop(int x, int y, int w, int h){
		this.srcImage = this.srcImage.getSubimage(x, y, w, h);
	}
	
	public void draw(BufferedImage img, int x, int y){
		this.srcImage.getGraphics().drawImage(img, x, y, null);
	}
	
	public void draw(String str, int x, int y){
		Graphics g = this.srcImage.getGraphics();
		if(this.font!=null){
			g.setFont(this.font);
		}
		if(this.color!=null){
			g.setColor(this.color);
		}
		g.drawString(str, x, y);
	}

	private int alphaBlend(int p1, int p2) {
		int r1 = p1>>>16&0x0000ff;
		int g1 = p1>>>8&0x0000ff;
		int b1 = p1&0x0000ff;
		int r2 = p2>>>16&0x0000ff;
		int g2 = p2>>>8&0x0000ff;
		int b2 = p2&0x0000ff;
		
		int r = r1 / 2 + r2 / 2;
		int g = g1 / 2 + g2 / 2;
		int b = b1 / 2 + b2 / 2;
		return (r<<16&0xff0000)|(g<<8&0x00ff00)|(b&0x0000ff);
	}

	public BufferedImage copy(){
		BufferedImage copyImage = new BufferedImage(srcImage.getWidth(), srcImage.getHeight(), srcImage.getType());
		for(int x=0;x<srcImage.getWidth();x++){
			for(int y=0;y<srcImage.getHeight();y++){
				int p = srcImage.getRGB(x, y);
				copyImage.setRGB(x, y, p);
				
				//info
				String argb = Integer.toHexString(p);
				String brg = Integer.toHexString((p & 0x00ffffff));
				int alpha = (p & 0xff000000)>>>56;
//				System.out.println( "argb="+ argb +",alph="+alpha+", rgb="+brg);
			}
		}
		return copyImage;
	}
	
	int previewX=100, previewY=100;
	void preview(){
		preview(this.srcImage);
	}
	void preview(final BufferedImage bimg){
		JFrame frame = new JFrame("test");
		frame.setBounds(previewX+=50, previewY+=50, 500, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		Canvas canvas = new Canvas() {
			public void paint(Graphics g) {
				g.drawImage(bimg, 0, 0, null);
			}
		};
		frame.add(canvas);
	}
	
	public void setFont(String name, int style, int size) {
		this.font = new Font(name, style, size);
	}

	public void setColor(int rgb) {
		this.color = new java.awt.Color(rgb);
	}

	public static void main(String[] args) throws Exception {
		String src = "D:\\temp\\speaker.jpg";
		String dest = "D:\\temp\\speaker11.jpg";
		ImageEditor myimg = new ImageEditor();
//		myimg.create(150, 100, BufferedImage.TYPE_INT_BGR);
		myimg.read(new File(src));
		BufferedImage img = myimg.copy();
//		myimg.preview(img);
		myimg.draw(img, 50, 50);
//		myimg.draw("hello", 0, 100);
//		myimg.zoom(200,200);
		myimg.preview();
	}
}
